package com.woniuxy.amusementparkos.controller.wyh;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.entity.wyh.Balance_Change;
import com.woniuxy.amusementparkos.entity.wyh.Customer;
import com.woniuxy.amusementparkos.entity.wyh.PageBean;
import com.woniuxy.amusementparkos.entity.wyh.WristStrap_Info;
import com.woniuxy.amusementparkos.entity.wyh.WristStrap_Repository;
import com.woniuxy.amusementparkos.service.wyh.Balance_ChangeService;
import com.woniuxy.amusementparkos.service.wyh.CustomerService;
import com.woniuxy.amusementparkos.service.wyh.WristStrap_InfoService;
import com.woniuxy.amusementparkos.service.wyh.WristStrap_RepositoryService;





@Controller
public class WriststrapInfoController {
	@Autowired
	WristStrap_InfoService wristStrap_InfoService;
	
	@Autowired
	Balance_ChangeService balance_ChangeService;
	
	@Autowired
	WristStrap_RepositoryService wristStrap_RepositoryService;
	@Autowired
	CustomerService customerService;
	
	//根据新 、旧腕带编号更换腕带。 并更新用户信息。
	@ResponseBody
	@RequestMapping("/doreplace")
	
	public Object doReplace(String oldwriststrapid,String newwriststrapid,Customer customer) {
		
		System.out.println(oldwriststrapid);
		
		System.out.println(newwriststrapid);
		
		System.out.println(customer);
		
		
		//简单的数据验证
		if(oldwriststrapid==null||newwriststrapid==null) {
			return null;
		};
		//判断旧腕带状态是否未使用状态
		WristStrap_Info workStateWristStrap = wristStrap_InfoService.getWorkStateWristStrap(oldwriststrapid);
		//如果没获取到直接返回
		if(workStateWristStrap==null) {
			return null;
		}
		//判断新腕带是否为闲置状态
		WristStrap_Repository wristStrap = wristStrap_RepositoryService.getWristStrap(newwriststrapid);
		//如果不为闲置状态，直接返回
		if(wristStrap.getState()!=0) {
			return null;
		}
		
		try {
			//执行更换腕带
			
			wristStrap_InfoService.doreplace(oldwriststrapid, newwriststrapid);
			
			//更改仓库腕带状态
			wristStrap_RepositoryService.updateState(oldwriststrapid, 0);
			wristStrap_RepositoryService.updateState(newwriststrapid, 1);
			
			customer.setCustomer_id(workStateWristStrap.getCustomer().getCustomer_id());
			//更新customer信息
			System.out.println(customer);
			customerService.updateCustomer(customer);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			//更换失败
			return null;
		}
			
		return "更换成功";
			
	}
	
	
	//根据条件查询腕带信息
	@ResponseBody
	@RequestMapping("/conditionQuerry")
	public PageBean<WristStrap_Info> selectBYconditon(WristStrap_Info wristStrap_Info,Integer pageindex,Integer size){
		PageBean<WristStrap_Info> byWristStrapId=null;
		System.out.println("成功了");
		System.out.println("接收到的腕带信息---------------"+wristStrap_Info);
		System.out.println("接收到的页面信息---------------"+pageindex);
		if(size==null) {
			size=10;
		}
		
		//如果没有页码信息，初始化为1
		if(pageindex==null) {
			pageindex=1;
			
		}
		//如果查看条件  腕带编号和电话号都为空  返回null
		// if(wristStrap_Info.getWriststrp_id()==null&&wristStrap_Info.getCustomer().getTel()==null) {
		//	return null;
		//}
		
		//如果腕带id不为空，根据腕带id查询相关的腕带信息
		//if(wristStrap_Info.getWriststrp_id()!=null) {
			
			//根据腕带号获取信息  返回值为类型集合
			try {
				 byWristStrapId = wristStrap_InfoService.selectByCondition(wristStrap_Info,pageindex,size);
				 if(byWristStrapId.getBeanList().size()==0) {
					 System.out.println("该用户信息不存在");
					 return null;
				 }
			} catch (Exception e) {
			//查询异常返回null， 表示未查询到相关信息
			return null;
			}
			System.out.println("腕带信息pagebean页面-----------："+byWristStrapId);
		//}
		return byWristStrapId;
		
	}
	
	//发放统计页面
	@RequestMapping("/showAll")
	public String queryInfo() {
		
		return "wyh/腕带发放统计.html";
	}
	
	//更换页面
	@RequestMapping("/replace")
	public String replace() {
		
		return "wyh/腕带更换.html";
		
	}
	
	//挂失页面
		@RequestMapping("/loss")
		public String loss() {
			
			return "wyh/腕带挂失.html";
			
		}
		
	//腕带挂失业务
		@ResponseBody
		@RequestMapping("/doloss")
		public Object doloss(String wriststrp_id) {
			
			try {
				//更改该腕带info状态
				wristStrap_InfoService.lossAndUnlock(wriststrp_id, 0, 2);
				//更改仓库腕带状态0闲置  1正在使用 2 挂失;
				wristStrap_RepositoryService.updateState(wriststrp_id, 2);
				

			} catch (Exception e) {
				return null;
			}
			return "挂失业务办理成功";
		}
		
		//腕带解锁业务
		@ResponseBody
		@RequestMapping("/doUnlock")
		public Object dounlock(String wriststrp_id) {
			
			try {
				//更改该腕带info状态
				wristStrap_InfoService.lossAndUnlock(wriststrp_id, 2, 0);
				//更改仓库腕带状态0闲置  1正在使用 2 挂失;
				wristStrap_RepositoryService.updateState(wriststrp_id, 1);
			} catch (Exception e) {
				return null;
			}
			return "解锁业务办理成功";
		}
		
	//结算页面
	@RequestMapping("/windup")
		public String windup() {
					
		return "wyh/腕带结算.html";
					
		}
	
	@ResponseBody
	//结算腕带信息
	@RequestMapping("/dowindup")
	public Object doWindup(String wriststrp_id,Double total) {
		System.out.println(wriststrp_id);
		
		System.out.println(total);
		
		//根据腕带编号获取正在使用的腕带信息;
		WristStrap_Info workStateWristStrap = wristStrap_InfoService.getWorkStateWristStrap(wriststrp_id);
		
		if(workStateWristStrap==null) {
			return null;
		}
		
		//设置当前时间为结算时间加入到workStateWristStrap；
		workStateWristStrap.setEndtime(new Date());
		
		//设置当前balance为0
		workStateWristStrap.setBalance(new BigDecimal(0));
		
		//修改 当前腕带信息状态为结算状态workStateWristStrap.
		workStateWristStrap.setInfo_state(1);
		
		try {
			
			//更新腕带信息
			
			wristStrap_InfoService.updateWindupMessage(workStateWristStrap);
			//更改腕带仓库腕带状态；
			wristStrap_RepositoryService.updateState(workStateWristStrap.getWriststrp_id(), 0);
			
			
			//total为结算总额。 加入到balance_changge,创建结算改变信息，加入到余额变动表.
			//
			Balance_Change balance_Change = new Balance_Change(null,workStateWristStrap.getInfo_id(),total,2,null,4,0,2);
			//结算信息录入到数据库;
			balance_ChangeService.addObject(balance_Change);
			
		} catch (Exception e) {

			return null;
		}
		
		
		return "成功";	
	}
	
	//余额退还
	@RequestMapping("/retroceded")
	public String retroceded() {
						
	return "wyh/余额退还.html";					
	}
}
